package com.ruoyi.project.his.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.his.domain.DoctorDept;
import com.ruoyi.project.his.service.IDoctorDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname DoctorDeptController
 * @Description
 * @Date 2022/6/27 16:09
 * @Author Lee
 */
@RestController
@RequestMapping("/his/dept")
public class DoctorDeptController extends BaseController {
    @Autowired
    private IDoctorDeptService doctorDeptService;

    @PreAuthorize("@ss.hasPermi('his:dept:list')")
    @GetMapping("/list")
    public TableDataInfo list(DoctorDept doctorDept){
        startPage();
        List<DoctorDept> doctorDepts = doctorDeptService.selectDoctorDeptList(doctorDept);
        return getDataTable(doctorDepts);
    }

    /**
     * @Author Lee
     * @Description 根据主键查询
     * @Date 2022/6/27 17:51
     * @Param  deptId
     * @return com.ruoyi.framework.web.domain.AjaxResult
     **/
    @PreAuthorize("@ss.hasPermi('his:dept:query')")
    @GetMapping("/{deptId}")
    public AjaxResult getInfo(@PathVariable Integer deptId){
        return AjaxResult.success(doctorDeptService.getDoctorDeptById(deptId));
    }


    @PreAuthorize("@ss.hasPermi('his:dept:add')")
    @Log(title = "科室管理", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult insert(@Validated @RequestBody DoctorDept doctorDept){
        if (doctorDeptService.checkDocketDeptNameUnique(doctorDept).equals(UserConstants.NOT_UNIQUE)) {
            return AjaxResult.error("新增科室'" + doctorDept.getDeptName() + "'失败，科室名称已存在");
        }

        if (doctorDeptService.checkDocketDeptCodeUnique(doctorDept).equals(UserConstants.NOT_UNIQUE)) {
            return AjaxResult.error("新增科室'" + doctorDept.getDeptCode() + "'失败，科室编码已存在");
        }
        doctorDept.setCreateBy(SecurityUtils.getUsername());
        return toAjax(doctorDeptService.insertDoctorDept(doctorDept));
    }

    @PreAuthorize("@ss.hasPermi('his:dept:edit')")
    @Log(title = "科室管理", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult update(@Validated @RequestBody DoctorDept doctorDept){
        if (doctorDeptService.checkDocketDeptNameUnique(doctorDept).equals(UserConstants.NOT_UNIQUE)) {
            return AjaxResult.error("修改科室'" + doctorDept.getDeptName() + "'失败，科室名称已存在");
        }

        if (doctorDeptService.checkDocketDeptCodeUnique(doctorDept).equals(UserConstants.NOT_UNIQUE)) {
            return AjaxResult.error("修改科室'" + doctorDept.getDeptCode() + "'失败，科室编码已存在");
        }
        doctorDept.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(doctorDeptService.updateDoctorDept(doctorDept));
    }

    @PreAuthorize("@ss.hasPermi('his:dept:remove')")
    @Log(title = "科室管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptIds}")
    public AjaxResult batchDelete(@PathVariable Integer[] deptIds){

        return toAjax(doctorDeptService.deleteDoctorDeptByDeptIds(deptIds));
    }

}
